package com.example;

import com.example.bean.Klass;
import com.example.bean.School;
import com.example.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lv
 * @date 2021/4/18 16:00
 */
@Configuration
@EnableConfigurationProperties(AutoConfigurationProperties.class)
@ConditionalOnClass({School.class})
@ConditionalOnProperty(prefix = "com.example", value = "enabled", matchIfMissing = true)
public class SchoolAutoConfiguration {

    @Autowired
    private AutoConfigurationProperties autoConfigurationProperties;
    private Student student = new Student();

    @Bean
    @ConditionalOnMissingBean(School.class)
    public School schoolConfiguration() {
        student.setId(autoConfigurationProperties.getId());
        student.setName(autoConfigurationProperties.getName());
        Klass klass = new Klass();
        List<Student> students = new ArrayList<Student>();
        students.add(student);
        klass.setStudents(students);
        School school = new School(klass,student);

        return school;
    }
}
